﻿#include "DATypes.h"

#include "DASetting.h"
#include "qdatetime.h"
#include "qobject.h"
#include "qsettings.h"
#include "qvariant.h"
#include <QFile>
#include <cstddef>

DASetting::DASetting(QObject *parent)
    :QObject(parent)
{
    Init();
}

int DASetting::GetNumberValue(const QString &key)
{
    return this->settings->value(key).toInt();
}

QString DASetting::GetStringValue(const QString &key)
{
    return this->settings->value(key).toString();
}

QSize DASetting::GetSizeValue(const QString &key)
{
    return this->settings->value(key).toSize();
}

QPoint DASetting::GetPointValue(const QString &key)
{
    return this->settings->value(key).toPoint();
}

QDateTime DASetting::GetDateValue(const QString &key)
{
    return this->settings->value(key).toDateTime();
}

void DASetting::SetValue(const QString &key,const QVariant &var)
{
    settings->setValue(key,var);
}

void DASetting::Default()
{
    DefaultConf[GENERAL_NAME]           = "DesktopAssist";
    DefaultConf[GENERAL_VERSION]        = "1.0";
    DefaultConf[GENERAL_SIZE]           = QSize(700,450);
    DefaultConf[GENERAL_POSITION]       = POSITION::Center;
    DefaultConf[GENERAL_DATEFORMAT]     = "yyyy年MM月dd日";
    DefaultConf[HLD_TITLE]              = QString("距离<font color=\"red\">❤️</font>五一");
    DefaultConf[HLD_DATE]               = "2023-04-29 00:00:00";
    
}



void DASetting::Init()
{
    Default();
    settings = new QSettings("desktop-assist","desktop-assist",this);
    settings->setDefaultFormat(QSettings::NativeFormat);
    
#if (QT_VERSION < QT_VERSION_CHECK(6,0,0))
    settings->setIniCodec("UTF8");
#endif
    
    QString conf = settings->fileName();
    if( !QFile::exists(conf) )
    {
        auto iter = DefaultConf.begin();
        for (; iter != DefaultConf.end(); ++iter) 
        {
            QString key = iter.key();
            QVariant var = iter.value();
            settings->setValue(key,var);
        }
        settings->sync();
        return ;
    }
    return ;
}
void DASetting::SaveConfig()
{
    settings->sync();
    
}